package gth.news.ctrl;

import gth.news.dto.NewsDto;
import gth.news.service.NewsService;
import org.junit.Test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NewsServlet extends HttpServlet {

    private NewsService newsService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = req.getPathInfo();
        System.out.println("doGet");
        System.out.println("pathInfo:" + pathInfo);
        System.out.println("RequestURL:" + req.getRequestURL().toString());

        newsService = new NewsService();

        if (pathInfo.equals("/list")) { //获取新闻列表
            List<NewsDto> all = null;
            try {
                all = newsService.findAll();
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            req.setAttribute("newsList",all);
            req.getRequestDispatcher("/views/newslist.jsp").forward(req, resp);
        } else if (pathInfo.matches("/[0-9]+$")) { //根据id查询
            Integer id;
            try {
                String sid = urlMatcher(pathInfo, "[0-9]+$").get(0);
                id = new Integer(sid);
                NewsDto dto = newsService.findById(id);
                req.setAttribute("news", dto);
                req.getRequestDispatcher("/views/news.jsp").forward(req, resp);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        System.out.println("--------------------------------");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doPost");
        System.out.println(req.getRequestURI());
        System.out.println(req.getRequestURL().toString());
        super.doPost(req, resp);
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("delete");
        System.out.println(req.getRequestURI());
        System.out.println(req.getRequestURL().toString());
        super.doDelete(req, resp);
    }

    @Test
    public void test() {
        List<String> strings = urlMatcher("/2013132", "[0-9]+$");
        System.out.println(strings.get(0));

    }

    /**
     * 提取有用信息
     *
     * @param url
     * @param regex
     * @return
     */
    private List<String> urlMatcher(String url, String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(url);
        List<String> matchList = new ArrayList<>();
        while (matcher.find()) {
            matchList.add(matcher.group());
        }
        return matchList;
    }
}
